######################################
###   makefile                     ###
###                                ###
###   Organización de Datos        ###
###   2do. cuatrimestre 2012       ###
###                                ###
###   TP - Entrega 2               ###
###                                ###
###   Grupo:                       ###
###                                ###
###   Orquera, Federico  (84333)   ###
###   Famiglietti, Bruno (85997)   ###
###   Torchio, Mariano   (85998)   ###
###                                ###
######################################

# Instrucciones para agregar una clase en alguna libreria:							\
 Ir al anexo de "Registro de Sources" y agregar los nombres de los .cpp que 		\
 tengan las clases que quieren agregar. Hay que agregarlos usando el path relativo  \
 al directorio de la capa ( miren los que ya hay cargados )

# Instrucciones de Uso
#
# + Para compilar las pruebas: make tests
#
# + Para compilar el tp (sin pruebas): make all
#
# + Para limpiar todo: make clean

# RECORDATORIO: POR FAVOR SIEMPRE REVISEN Q TODO COMPILE ANTES DE SUBIR ALGO AL SVN
	
########################################################################
########################################################################
#######    DEFINICIÓN DE VARIABLES DE CONFIGURACIÓN (NO TOCAR)   #######
########################################################################
########################################################################

# Variables de Configuración
SHELL = /bin/sh
VPATH = $(OBJ_DIR) $(BIN_DIR) $(TEST_DIR)
CXX = g++
ARCHIVER = ar
AFLAGS = -r
CXXARGS = -Wall -fmessage-length=0 -std=c++0x
CXXFLAGS = -c 
LDFLAGS = -I$(PHYSICAL_DIR) -I$(LOGIC_DIR)
RM = rm
RMFLAGS = -f -r
MKDIR = mkdir
MKDIRFLAGS = -p
CP = cp

# Directorios

TEMP_DIR = temp/
GTEST_DIR = extras/libs/gtest/
RUNTEST_DIR = extras/runtest/
TOOLS_DIR	= extras/tools/
TESTFILES_DIR = extras/testfiles/
OBJ_DIR = obj/
BIN_DIR = bin/
TEST_DIR = test/
SRC_DIR = src/
TEST_SRC_DIR = $(SRC_DIR)test/
PHYSICAL_DIR = $(SRC_DIR)physical_layer/
LOGIC_DIR  = $(SRC_DIR)logic_layer/
APPLICATION_DIR = $(SRC_DIR)application_layer/
SOURCES_FP = 

########################################################################
########################################################################
#######################    Registro de Sources    ######################
########################################################################
########################################################################

# Sources de la capa Física:
PHYSICAL_S = common_components/BitMapFreeSpaceManager.cpp \
			 common_components/FixedSizeBlockFile.cpp \
			 common_components/BlockFileWithByteAccess.cpp \
			 file_system/Phrase.cpp \
			 file_system/PhraseDictionary.cpp \
			 file_system/Term.cpp \
			 file_system/TermSequentialFile.cpp \
			 file_system/TermOccurrence.cpp \
			 file_system/TermOccurrenceSequentialFile.cpp \
			 serializers/VariableStringSerializer.cpp \
			 serializers/PhraseSerializer.cpp \
			 serializers/TermSerializer.cpp \
 			 serializers/InvertedListSerializer.cpp \
			 file_system/InvertedListRecord.cpp \
			 serializers/InvertedListRecordSerializer.cpp \
			 serializers/InvertedListRecordListSerializer.cpp \
			 file_system/InvertedListFile.cpp \
			 compresorGammaElias/ColeccionDeBits.cpp \
		  	 compresorGammaElias/Compresor.cpp \
		     compresorGammaElias/Descompresor.cpp \
		     compresorGammaElias/Utilidades.cpp \
		     compresorGammaElias/ColeccionDeBytes.cpp \
		     compresorGammaElias/Byte.cpp \
		     compresorGammaElias/GammaElias.cpp \
			 # fin de lista de sources de la capa física
			 
# Sources de la capa Lógica:
LOGIC_S = \
		  bplustree/bplusfilehandler/FrontCodingEncoder.cpp \
		  bplustree/bplusfilehandler/LeafRegisterSerializer.cpp \
		  bplustree/bplusfilehandler/InternalRegisterSerializer.cpp \
		  bplustree/bplusfilehandler/LeafNodeSerializer.cpp \
  		  bplustree/bplusfilehandler/InternalNodeSerializer.cpp \
		  bplustree/bplusfilehandler/BPlusTreeDefinitions.cpp \
		  bplustree/bplusfilehandler/BPlusTreeFile.cpp \
		  external_sort/ExternalSort.cpp \
		  external_sort/FileManager.cpp \
		  external_sort/FileMerger.cpp \
		  # \
          # fin de lista de sources de la capa logica

# Sources de la capa de Aplicación:
APPLICATION_S = \
	# fin de la lista de sources de la capa de aplicación


########################################################################
########################################################################
################    A PARTIR DE ACÁ NO TOCAR NADA !    #################
########################################################################
########################################################################

# Librerias
LIBS = libphysical.a liblogic.a

# Lógica de parseo de dependencias y demás
PHYSICAL_S_FP = $(foreach source, $(PHYSICAL_S), $(addprefix $(PHYSICAL_DIR), $(source) ) )
PHYSICAL_O = $(patsubst %.cpp, %.o, $(notdir $(PHYSICAL_S_FP)) )
override SOURCES_FP += $(PHYSICAL_S_FP)

LOGIC_S_FP = $(foreach source, $(LOGIC_S), $(addprefix $(LOGIC_DIR), $(source) ) )
LOGIC_O = $(patsubst %.cpp, %.o, $(notdir $(LOGIC_S_FP)) )
override SOURCES_FP += $(LOGIC_S_FP)

APPLICATION_S_FP = $(foreach source, $(APPLICATION_S), $(addprefix $(APPLICATION_DIR), $(source) ) )
APPLICATION_O = $(patsubst %.cpp, %.o, $(notdir $(APPLICATION_S_FP)) )
override SOURCES_FP += $(APPLICATION_S_FP)

override VPATH += $(dir $(SOURCES_FP))

ifneq ($(MAKECMDGOALS),clean)
    -include $(PHYSICAL_O:%.o=$(OBJ_DIR)%.d) $(LOGIC_O:%.o=$(OBJ_DIR)%.d) $(APPLICATION_O:%.o=$(OBJ_DIR)%.d) $(TEMP_DIR)gtest_main.d
endif

# Targets del Makefile

.PHONY: all clean createDir executables tests tools copyFiles
	
executables: phraseMng booleanIndex signatureSliceIndex dumpPhraseDictionary
	
all: override CXXARGS+= -O2
all: override CXXFLAGS+= -O2
all: createDirs $(LIBS) executables copyFiles
	
all-debug: override CXXARGS+= -O0 -g3
all-debug: override CXXFLAGS+= -O0 -g3
all-debug: createDirs $(LIBS) executables copyFiles
	
copyFiles:
	@$(CP) frases_celebres.csv bin
	@$(CP) stop-words.txt bin
	@$(CP) construirIndices.sh bin
	@$(CP) estadisticas.sh bin
	
createDirs:
	@$(MKDIR) $(MKDIRFLAGS) $(BIN_DIR) $(OBJ_DIR) $(TEST_DIR) $(TEMP_DIR)
	
clean: 
	@$(RM) $(RMFLAGS) $(BIN_DIR)* $(OBJ_DIR)* $(TEST_DIR)* $(TEMP_DIR)*	
	
tests: override CXXARGS+= -O0 -g3
tests: override CXXFLAGS+= -O0 -g3
tests: createDirs $(TEST_DIR)gtest_test \
	# fin de tests targets

tools: createDirs \
	# fin de tools targets

#executables
phraseMng: $(APPLICATION_DIR)PhraseFileManager.cpp obj/phraseMng.d liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(BIN_DIR)$@" "$<" -llogic -lphysical

booleanIndex: $(APPLICATION_DIR)booleanIndex.cpp obj/booleanIndex.d liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(BIN_DIR)$@" "$<" -llogic -lphysical

signatureSliceIndex: $(APPLICATION_DIR)signatureSliceIndex.cpp obj/signatureSliceIndex.d liblogic.a libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(BIN_DIR)$@" "$<" -llogic -lphysical

dumpPhraseDictionary: $(TOOLS_DIR)dumpPhraseDictionary.cpp libphysical.a
	$(CXX) $(LDFLAGS) $(CXXARGS) -L"$(OBJ_DIR)" -o "$(BIN_DIR)$@" "$<" -lphysical
	
# Dependencias Automáticas De ejecutables
obj/phraseMng.d : $(APPLICATION_DIR)PhraseFileManager.cpp $(APPLICATION_DIR)application_commons.h
	$(CXX) -MM -MP -MF"$@" $(APPLICATION_DIR)PhraseFileManager.cpp
	
obj/booleanIndex.d : $(APPLICATION_DIR)booleanIndex.cpp $(APPLICATION_DIR)application_commons.h
	$(CXX) -MM -MP -MF"$@" $(APPLICATION_DIR)booleanIndex.cpp

obj/signatureSliceIndex.d : $(APPLICATION_DIR)signatureSliceIndex.cpp $(APPLICATION_DIR)application_commons.h
	$(CXX) -MM -MP -MF"$@" $(APPLICATION_DIR)signatureSliceIndex.cpp

# Tests

$(TEST_DIR)gtest_test: $(LIBS) $(TEMP_DIR)all_gtests.h $(TEMP_DIR)gtest_main.d
	$(CXX) $(LDFLAGS) $(CXXARGS) -I$(GTEST_DIR) -L$(GTEST_DIR) -L"$(OBJ_DIR)" -o $(TEST_DIR)gtest_test $(TEMP_DIR)gtest_main.cpp -llogic -lphysical -lgtest -lpthread

$(TEMP_DIR)all_gtests.h: extras/gtest_main.cpp $(wildcard $(TEST_SRC_DIR)*_gtest.h)
	@$(CP) extras/gtest_main.cpp $(TEMP_DIR)
	@ls -1 $(TEST_SRC_DIR)*_gtest.h | sed -e 's-\(.*\)-\#include "../\1"-' > $(TEMP_DIR)all_gtests.h
	
runtest: $(RUNTEST_DIR)runtest.cc
	$(CXX) $(CXXARGS) -o "$(TEST_DIR)runtest" "$<"
	
# Librerias

libphysical.a: $(PHYSICAL_O)
	$(ARCHIVER) $(AFLAGS) "$(OBJ_DIR)libphysical.a" $(addprefix $(OBJ_DIR), $(PHYSICAL_O))
	
liblogic.a: $(LOGIC_O)
	$(ARCHIVER) $(AFLAGS) "$(OBJ_DIR)liblogic.a" $(addprefix $(OBJ_DIR), $(LOGIC_O))
	
libapplication.a: $(APPLICATION_O)
	$(ARCHIVER) $(AFLAGS) "$(OBJ_DIR)libapplication.a" $(addprefix $(OBJ_DIR), $(APPLICATION_O))

# Dependencias Automáticas
$(OBJ_DIR)%.d : %.cpp
	$(CXX) -MM -MP -MF"$@" "$(filter %$*.cpp, $(SOURCES_FP))"
	
$(TEMP_DIR)gtest_main.d: $(TEMP_DIR)all_gtests.h
	$(CXX) -MM -MP -MF"$(TEMP_DIR)gtest_main.d" -MT"$(TEST_DIR)gtest_test" "$(TEMP_DIR)gtest_main.cpp"

# Objetos
%.o : %.cpp %.h %.d
	$(CXX) $(CXXFLAGS) $(CXXARGS) -o"$(OBJ_DIR)$@" "$(filter %$*.cpp, $(SOURCES_FP))"
